package com.sword.service.impl.stock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.pojo.stock.EnterstockDetail;
import com.sword.service.impl.BasicBaseServiceImpl;
import com.sword.service.stock.EnterStockDetailService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Date;

@Service
public class EnterStockDetailServiceImpl extends BasicBaseServiceImpl<EnterstockDetail> implements EnterStockDetailService {
    @Override
    public PageResult queryEnterStockDetail(QueryPageBean queryPageBean) {
        QueryWrapper<EnterstockDetail> wrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        // 如果queryString有值
        if (queryString != null) {
            EnterstockDetail enterStockDetail = new EnterstockDetail();
            Field[] fields = enterStockDetail.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                //有的字段是用private修饰的 将他设置为可读
                fields[i].setAccessible(true);
                if (fields[i].getName().endsWith("Date")) {
                    continue;
                }
                wrapper.like(fields[i].getName(), queryString).or();
            }
        }
        IPage<EnterstockDetail> iPage = super.queryPageList(wrapper, queryPageBean.getPage(), queryPageBean.getLimit());
        return new PageResult(0, MessageConstant.QUERY_ENTERSTOCKDETAIL_SUCCESS, iPage.getTotal(), iPage.getRecords());
    }


    @Override
    public Integer addEnterStockDetail(EnterstockDetail enterStockDetail) {
        enterStockDetail.setCreateDate(new Date());
        return super.save(enterStockDetail);
    }

    @Override
    public Integer editEnterStockDetail(EnterstockDetail enterStockDetail) {
        return super.update(enterStockDetail);
    }

    @Override
    public Integer examineEnterStockDetail(EnterstockDetail enterStockDetail) {
        //获取当前用户id
        String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        enterStockDetail.setExamineid(userId);
        enterStockDetail.setStatus(1);
        return super.update(enterStockDetail);
    }

    @Override
    public Integer unexamineEnterStockDetail(EnterstockDetail enterStockDetail) {
        enterStockDetail.setExamineid("");
        enterStockDetail.setStatus(0);
        return super.update(enterStockDetail);
    }

    @Override
    public Integer deleteEnterStockDetail(Integer id) {
        return super.deleteById(Long.valueOf(id));
    }
}
